<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理/新增编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">
    <link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>
    <link rel="stylesheet" href="${ctx}/css/public.css">
    <style>
        .layui-layout-body{overflow: auto;}
        .layui-fluid{height: auto !important;}
    </style>
    <script type="text/javascript">
        var __ctx = "${ctx}";
    </script>
</head>
<body>
    <div class="layui-fluid" style="height: 520px;overflow: auto;padding: 0;background: #fff;">
        <div class="layui-card-body">
            <form class="layui-form" action="" lay-filter="roleForms">
                <div class="layui-form-item">
                    <label class="layui-form-label">角色名称:<span style="color:red">*</span></label>
                    <div class="layui-input-block" style="width:30%">
                        <input type="text" name="roleName" autocomplete="off" class="layui-input rolename" maxlength="32" lay-verify="required|names">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色别名:<span style="color:red">*</span></label>
                    <div class="layui-input-block" style="width:30%">
                        <input type="text" name="alias" maxlength="32" autocomplete="off"
                               class="layui-input alias" lay-verify="required|alias">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色分类:<span style="color:red">*</span></label>
                    <div class="layui-input-block">
                        <input type="text" name="category" maxlength="32" autocomplete="off" class="layui-input" style="width:34.5%;float: left" lay-verify="required|category">
                        <div style="width: 36%;display: inline-block;margin-left: 20px">
                            <select id="categorys" lay-filter="categorys">
                                <option value=""></option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">子系统名称:<span style="color:red">*</span></label>
                    <div class="layui-input-block" style="width: 34%;">
                        <select name="systemId" id="systemIdSel" lay-verify="required" <#if roleId??>disabled</#if>>
                            <option value=""></option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注:</label>
                    <div class="layui-input-block">
                        <textarea name="memo" class="layui-textarea" maxlength="256"></textarea>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <label class="layui-form-label">允许删除:</label>
                    <div class="layui-input-block">
                        <input type="radio" name="allowDel" value="1" title="允许" checked>
                        <input type="radio" name="allowDel" value="0" title="不允许">
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <label class="layui-form-label">允许编辑:</label>
                    <div class="layui-input-block">
                        <input type="radio" name="allowEdit" value="1" title="允许" checked>
                        <input type="radio" name="allowEdit" value="0" title="不允许">
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <label class="layui-form-label">是否公开:</label>
                    <div class="layui-input-block">
                        <input type="radio" name="isPublic" value="1" title="允许">
                        <input type="radio" name="isPublic" value="0" title="不允许" checked>
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <label class="layui-form-label">是否启用:</label>
                    <div class="layui-input-block">
                        <input type="radio" name="enabled" value="1" title="启用" checked>
                        <input type="radio" name="enabled" value="0" title="禁用">
                    </div>
                </div>
                <div class="layui-col-xs6">
                    <label class="layui-form-label">是否切换:</label>
                    <div class="layui-input-block">
                        <input type="radio" name="enableSwitch" value="1" title="启用" checked>
                        <input type="radio" name="enableSwitch" value="0" title="禁用">
                    </div>
                </div>
                <input type="hidden" id="tenantId" name="tenantId" style="display: none;" />
                <div class="layui-form-item" style="padding-top: 20px;margin-bottom: 0">
                    <div class="layui-input-block" style="text-align: center;margin: 0">
                        <a lay-submit lay-filter="roleSub" class="layui-btn">保存</a>
                        <a class="layui-btn cancelAddedit layui-btn-danger">取消</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script src="${ctx}/public/component.js" type="text/javascript"></script>
    <script src="${ctx}/layui/layui.js"></script>
    <script>
        layui.config({
            base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
        }).extend({
            index: 'index' //主入口模块
        }).use(['index', 'setter', 'commons', 'form', 'jquery', 'layer', 'table', 'totalFilter'], function () {
            var form = layui.form, layer = layui.layer, commons = layui.commons, setter = layui.setter, $ = layui.jquery,
                totalFilter = layui.totalFilter;
            var roleId = '${roleId}';
            var selectOrgId = '${selectRoleId}';//新增
            //初始化   子系统select
            queryActiveSystem();
            form.verify(commons.formVerify);

            function queryActiveSystem() {
                $.ajax({
                    url: serverPath.systemPath + setter.sysLinkPath + "subSystem/queryActiveSystem_simple",
                    type: "get",
                    contentType: 'application/json',
                    dataType: 'json',
                    async: false,
                    success: function (data) {
                        if (commons._filterRecord(data)) {
                            var datas = data.data;
                            var systemData = "";
                            for (var i = 0; i < datas.length; i++) {
                                systemData += "<option value='" + datas[i].value + "'>" + datas[i].name + "</option>";
                            }
                            $("#systemIdSel").append(systemData)
                        } else {
                            layer.alert(data.msg);
                        }
                    },
                });
            }

            //监听提交
            if (roleId) {
                form.on('submit(roleSub)', function (data) {
                    data.field.roleId = roleId;
                    $.ajax({
                        url: serverPath.systemPath + setter.sysLinkPath + "sysRole/add",
                        type: "POST",
                        contentType: 'application/json',
                        dataType: 'json',
                        data: JSON.stringify(data.field),
                        success: function (data) {
                            if (commons._filterRecord(data)) {
                                layer.alert(data.msg, function (index) {
                                    layer.close(index);
                                    parent.layui.table.reload('roleListTable', {
                                        where: {orgId: selectOrgId},
                                        page: {curr: 1}
                                    });
                                    parent.layui.totalFilter.init('roleListTable');
                                    parent.layui.Initializations();
                                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                                });
                            } else {
                                layer.alert(data.msg);
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            layer.alert("保存失败！" + XMLHttpRequest.status);
                        },
                    });
                });
                //数据回写
                $.ajax({
                    url: serverPath.systemPath + setter.sysLinkPath + "sysRole/get",
                    type: "get",
                    contentType: 'application/json',
                    dataType: 'json',
                    data: {
                        roleId: roleId
                    },
                    success: function (data) {
                        var returnDate = data.data;
                        form.val('roleForms', {
                            "roleName": returnDate.roleName,// "name": "value"
                            "alias": returnDate.alias,//复选框选中状态
                            "category": returnDate.category,
                            "memo": returnDate.memo,
                            "allowDel": returnDate.allowDel + "",
                            "allowEdit": returnDate.allowEdit + "",
                            "isPublic": returnDate.isPublic + "",
                            "enabled": returnDate.enabled + "",
                            "enableSwitch": returnDate.enableSwitch + "",
                            "systemId": returnDate.systemId + "",
                            "tenantId": returnDate.tenantId
                        });
                    }
                });
            } else {
                form.on('submit(roleSub)', function (data) {
                    if (selectOrgId) {
                        data.field.orgId = selectOrgId;
                    } else {
                        data.field.orgId = "";
                    }
                    $.ajax({
                        url: serverPath.systemPath + setter.sysLinkPath + "sysRole/add",
                        type: "POST",
                        contentType: 'application/json',
                        dataType: 'json',
                        data: JSON.stringify(data.field),
                        success: function (data) {
                            if (commons._filterRecord(data)) {
                                layer.alert(data.msg, function (index) {
                                    layer.close(index);
                                    parent.layui.table.reload('roleListTable', {
                                        where: {orgId: selectOrgId},
                                        page: {curr: 1}
                                    });
                                    parent.layui.totalFilter.init('roleListTable');
                                    parent.layui.Initializations();
                                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                                });
                            } else {
                                layer.alert(data.msg);
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            layer.alert("保存失败！" + XMLHttpRequest.status);
                        },
                    });
                });
            }
            $(".cancelAddedit").click(function () {
                parent.layer.close(parent.layer.getFrameIndex(window.name));
            });
            //初始化   角色分类select
            reflashOrgSelect();

            function reflashOrgSelect() {
                $.ajax({
                    url: serverPath.systemPath + setter.sysLinkPath + "sysRole/queryDistinctCategory",
                    type: "get",
                    contentType: 'application/json',
                    dataType: 'json',
                    data: {
                        orgId: selectOrgId ? selectOrgId : "",
                        roleId: roleId ? roleId : ""
                    },
                    success: function (data) {
                        if (commons._filterRecord(data)) {
                            var datas = data.data;
                            var optionDatas = "";
                            for (var i = 0; i < datas.length; i++) {
                                optionDatas += "<option value='" + datas[i] + "'>" + datas[i] + "</option>"
                            }
                            $("#categorys").append(optionDatas);
                            form.render('select');
                        } else {
                            layer.alert(data.msg);
                        }
                    },
                });
            }

            form.on('select(categorys)', function (data) {
                $("input[name='category']").val(data.value);
            });

            //自动生成别名
            $(".rolename").change(function () {
                var str = $(this).val();
                if (str == "") return;
                var arrRslt = component.chineseToPinyinAlias(str);
                $(".alias").val(arrRslt[0]);
            });

        });
    </script>
</body>
</html>